package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 蝙蝠魅影<br/>
 * 释放蝙蝠飞镖对敌方全体造成{0=130#1}%技能伤害，并造成出血效果，每回合损失{1=2#0}%HP持续{2=5#0}回合
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90131 extends CombatSkill {

	public COMBAT_SKILL_90131(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		List<CombatUnit> targets = action.target.all();
		float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
		for (int i = 0; i < targets.size(); i++) {
			CombatUnit target = targets.get(i);
			if (!target.isDied()) {
				ActionEffect eff = action.addTarget(target);
				target = eff.getUnit(); // fix the target
				CombatUtils.calcMagicAttack(eff, rate);
				action.addBuff(new BUFF(skill, target, (int) source.getAttackPower()));
			}
		}
	}
	
	static class BUFF extends CombatBuff {
		int hpDelta;
		public BUFF(Skill skill, CombatUnit owner, int damageMax) {
			super(skill.getTemplate().getCombatBuff(), owner, (int) skill.getParameter(2));
			int val = (int) (owner.getMaxHp() * skill.getParameter(1) / GameStaticConfig.percentBase);
			if (val > damageMax) {
				val = damageMax;
			}
			hpDelta = 0 - Math.max(val, 1);
		}
		@Override
		public void preAction(Action action) {
			active = true;
			ActionEffect effect = action.addTarget(owner);
			effect.effect(CombatAttributeType.HP, hpDelta);
		}
		@Override
		@JSONField(serialize = false)
		public boolean isDebuff() {
			return true;
		}
	}

}
